﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using MQTTnet.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.MQTT.Server.Hosts
{
    public class MQTTHost
    {
        public MQTTHost()
        {
            StartHost();
        }

        public static Task StartHostSingle()
        {
            MQTTHost mQTTHost = new MQTTHost();
           return mQTTHost.StartHost();
        }

        private Task StartHost()
        {
            /*
                  * This sample starts a minimal ASP.NET Webserver including a hosted MQTT server.
                  */
            var host = Host.CreateDefaultBuilder(Array.Empty<string>())
                .ConfigureWebHostDefaults(
                    webBuilder =>
                    {
                        webBuilder.UseKestrel(
                            o =>
                            {
                                // This will allow MQTT connections based on TCP port 1883.
                                o.ListenAnyIP(1883, l => l.UseMqtt());

                                // This will allow MQTT connections based on HTTP WebSockets with URI "localhost:5000/mqtt"
                                // See code below for URI configuration.
                                o.ListenAnyIP(5000); // Default HTTP pipeline
                            });

                        webBuilder.UseStartup<Startup>();
                    });

            return host.RunConsoleAsync();
        }


    }
}
